package quest
{
import core.model.GameModel;
import core.model.QuestModel;

import flash.errors.IllegalOperationError;

import quest.handler.QuestHandler;
import quest.trigger.QuestTrigger;
import quest.uiproxy.IQuestUIProxy;
import quest.uiproxy.QuestUIProxy;

import values.quest.QuestGoalValue;
import values.quest.QuestValue;

/**
 * QuestManager
 * @author jliang
 * 
 */
public class QuestManager implements IQuestManager
{	
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function QuestManager(enforce:SingletonEnforce)
	{
		if (!enforce)
		{
			throw new IllegalOperationError("QuestManager is a singleton class.");
		}
		handler = new QuestHandler();
		trigger = new QuestTrigger();
		model = GameModel.instance.questModel;
		
		ui = new QuestUIProxy();
	}
	//==========================================================================
	//  Class constants
	//==========================================================================
	private static var _instance:IQuestManager;
	
	public static function get instance():IQuestManager
	{
		if (!_instance)
		{
			_instance = new QuestManager(new SingletonEnforce());
		}
		return _instance;
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	private var handler:QuestHandler;
	
	private var trigger:QuestTrigger;
	
	private var model:QuestModel;
	
	private var ui:IQuestUIProxy;
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function checkNextQuest():void
	{
	}
	
	public function checkGameAction(type:String, ...args):void
	{
		handler.check.apply(handler, [ type ].concat(args));
		trigger.check.apply(trigger, [ type ].concat(args));
	}
	
	public function questAppear(questValue:QuestValue):void
	{
		for each (var goal:QuestGoalValue in questValue.goals)
		{
			handler.addTargetQuestGoal(goal);
		}
		
		model.addQuest(questValue);		
		trace("appear quest: " + questValue.template.key);
		
		ui.questAppear(questValue);		
		questValue.appear();
	}
	
	public function questAccept(questValue:QuestValue):void
	{
		ui.questAccept(questValue);
		questValue.accept();
		
		trace("accept quest: " + questValue.template.key);
	}
	
	public function viewQuestDetail(questValue:QuestValue):void
	{
		ui.viewQuestDetail(questValue);
		questValue.viewDetail();
	}
	
	public function questProgress(questValue:QuestValue):void
	{
		ui.questProgress(questValue);
		questValue.update();
		
		trace("update quest: " + questValue.template.key);
	}
	
	public function questGoalComplete(goal:QuestGoalValue):void
	{
		goal.isCompleted = true;
		if (goal.owner.isCompleted)
		{
			questComplete(goal.owner);
		}
	}
	
	public function questComplete(questValue:QuestValue):void
	{
		ui.questComplete(questValue);
		trace("complete quest: " + questValue.template.key);
		for each (var goal:QuestGoalValue in questValue.goals)
		{
			handler.removeTargetQuestGoal(goal);
		}
		questValue.complete();
		
		trigger.checkNextQuest(questValue);
	}
}
}

class SingletonEnforce
{
	
}